/**
 * app的版本分两种，一种是原生版本，一种是内容版本，应用内更新的都是针对内容版本，
 * 与原生版本无关，我们以后讨论的就是内容版本
 */
var deviceone = require("deviceone");
var app = deviceone.sm("do_App");
var page = deviceone.sm("do_Page");
var global = deviceone.sm("do_Global");
var storage = deviceone.sm("do_Storage");
var external = deviceone.sm("do_External");
var cacher = deviceone.sm("do_DataCache");
var nf = deviceone.sm("do_Notification");

var httpSilence = require("helper/httpSilence");
var openHelper = require("helper/open");
var qqCacher = require("helper/qqCacher");

/** 下载apk或ipa时的存放目录地址 **/
var appInstallNewUri = "data://upgrade/temp/install";
/** 应用内更新包的存放地址 **/
var appUpdateNewUri = "data://upgrade/temp/new.zip";
/** 应用内更新时，文件解压的存放地址 **/
var appUpdateUnzipUri = "data://upgrade/temp/update";

module.exports.appInstallNewUri = function(){
	return appInstallNewUri;
};
module.exports.appUpdateNewUri = function(){
	return appUpdateNewUri;
};

app.on("fireAppUpgrade",function(data){
	if(data&&data!=null){
		//deviceone.print("update data==="+JSON.stringify(data));
		doUpgrade(data);
	}
});
function doUpdateByVersion(updFile,toVersion,doDnuCb){
	if(updFile && updFile!=null && updFile !==""
		&&toVersion && toVersion!=null && toVersion !==""){
		downloadNUpdateSilence(updFile,toVersion,doDnuCb);
	}
}
module.exports.doUpdateByVersion = doUpdateByVersion;

function doDownloadNUpdateSilence(pubUpgrade,doDnuCb){
	if(pubUpgrade && pubUpgrade!=null && pubUpgrade !=="undefined"){
		if(pubUpgrade.isUpgrade==1){
			var updFile = pubUpgrade.updFile;
			var updVersion = pubUpgrade.updVersion;
			var needUpdContent = isNewVersion(qqCacher.currentContentVersion(),updVersion);
			if(!updFile||updFile===""||updFile==="undefined"){
				needUpdContent = false;
			}
			if(needUpdContent==true){
				downloadNUpdateSilence(updFile,updVersion,doDnuCb);
			}
		}
	}
}
module.exports.doDownloadNUpdateSilence = doDownloadNUpdateSilence;

function doUpgrade(pubUpgrade){
	if(pubUpgrade && pubUpgrade!=null && pubUpgrade !=="undefined"){
		if(pubUpgrade.isUpgrade==1){
			var updFile = pubUpgrade.updFile;
			var marketFile = pubUpgrade.marketFile;
			var isSilence = pubUpgrade.isSilence;
			var isUpdContentOnly = pubUpgrade.isUpdContentOnly;
			
			var nativeVersion = pubUpgrade.nativeVersion;
			var leastNativeVersion = pubUpgrade.leastNativeVersion;
			var updVersion = pubUpgrade.updVersion;
			
			var needMustMarket = isNewVersion(qqCacher.currentNativeVersion(),leastNativeVersion);
			var needMarket = isNewVersion(qqCacher.currentNativeVersion(),nativeVersion);
			if(!marketFile||marketFile===""||marketFile==="undefined"){
				needMustMarket = false;
				needMarket = false;
			}
			var needUpdContent = isNewVersion(qqCacher.currentContentVersion(),updVersion);
			if(!updFile||updFile===""||updFile==="undefined"){
				needUpdContent = false;
			}
			if(needMustMarket==true||needMarket==true||needUpdContent==true){
				if(needMustMarket==false&&needMarket==false&&needUpdContent==true&&isSilence==1){
					downloadNUpdateSilence(updFile,updVersion);
				}else{
					var upgradeDataJson = {"needUpdContent":needUpdContent,"updFile":updFile,"updVersion":updVersion};
					upgradeDataJson.isUpdContentOnly = isUpdContentOnly;
					upgradeDataJson.needMustMarket = needMustMarket;
					upgradeDataJson.needMarket = needMarket;
					upgradeDataJson.marketFile = marketFile;
					upgradeDataJson.title = pubUpgrade.title;
					upgradeDataJson.contentList = pubUpgrade.contentList;
					openHelper.openUpgrade(upgradeDataJson);
				}
			}
		}
	}
}
module.exports.doUpgrade = doUpgrade;

function updateContent(toContentVersion,updateCb){
	var needUpdContent = isNewVersion(qqCacher.currentContentVersion(),toContentVersion);
	if(needUpdContent==true){
		storage.unzip(
	    	appUpdateNewUri,appUpdateUnzipUri,
	    	function(data){
	    		app.update(
	    			[appUpdateUnzipUri],
	    			"source://",
	    			function(){
	    				qqCacher.firstTimeOpen("1");
	                    if(toContentVersion && toContentVersion!==""){
	                    	qqCacher.currentContentVersion(toContentVersion);
	                    }
	                    if(updateCb && typeof updateCb == "function") {
	                    	updateCb();
	                    }
	                }
	    		);
	    	}
	    );
	}
}
module.exports.updateContent = updateContent;

app.on("fireDownloadNUpdateSilence",function(data){
	if(data&&data!=null){
		downloadNUpdateSilence(data.updFile,data.updVersion);
	}
});

function downloadNUpdateSilence(upgradeFile,toContentVersion,dnuCb){
	var needUpdContent = isNewVersion(qqCacher.currentContentVersion(),toContentVersion);
	if(upgradeFile&&upgradeFile!==""&&needUpdContent==true){
		httpSilence.download(upgradeFile,appUpdateNewUri,
			function(data){
				updateContent(toContentVersion,function(){
					if(dnuCb && typeof dnuCb == "function") {
						dnuCb();
                    }
				});
			}
		);
	}
}
module.exports.downloadNUpdateSilence = downloadNUpdateSilence;
/** 如果后面的版本号比前面的大，则返回true,如果小，则返回false **/
function isNewVersion(oldVer,newVer){
	if((!oldVer||oldVer=="")&&newVer&&newVer!==""){
		return true
	}
	var oldVerArr = oldVer.split(".");
	var newVerArr = newVer.split(".");
	if((oldVerArr==null||oldVerArr.length==0)&&newVerArr!=null&&newVerArr.length>0){
		return true;
	}
	for(var i=0;i<newVerArr.length;i++){
		if(i<oldVerArr.length){
			if(parseInt(oldVerArr[i])<parseInt(newVerArr[i])){
				return true;
			}else if(parseInt(oldVerArr[i])>parseInt(newVerArr[i])){
				return false;
			}
		}else{
			return false;
		}
	}
	return false;
}
module.exports.isNewVersion = isNewVersion;